package com.yzxb.messystem.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class LogUtils {
    
    private static final String DEFAULT_BUSINESS_TYPE = "DEFAULT";
    private static final String DEFAULT_OPERATION_TYPE = "UNKNOWN";
    
    // 记录调试日志
    public static void debug(Class<?> clazz, String message) {
        Logger logger = LoggerFactory.getLogger(clazz);
        if (logger.isDebugEnabled()) {
            logger.debug(message);
        }
    }
    
    // 记录调试日志，带参数
    public static void debug(Class<?> clazz, String message, Object... args) {
        Logger logger = LoggerFactory.getLogger(clazz);
        if (logger.isDebugEnabled()) {
            logger.debug(message, args);
        }
    }
    
    // 记录信息日志
    public static void info(Class<?> clazz, String message) {
        Logger logger = LoggerFactory.getLogger(clazz);
        if (logger.isInfoEnabled()) {
            logger.info(message);
        }
    }
    
    // 记录信息日志，带参数
    public static void info(Class<?> clazz, String message, Object... args) {
        Logger logger = LoggerFactory.getLogger(clazz);
        if (logger.isInfoEnabled()) {
            logger.info(message, args);
        }
    }
    
    // 记录警告日志
    public static void warn(Class<?> clazz, String message) {
        Logger logger = LoggerFactory.getLogger(clazz);
        if (logger.isWarnEnabled()) {
            logger.warn(message);
        }
    }
    
    // 记录警告日志，带参数
    public static void warn(Class<?> clazz, String message, Object... args) {
        Logger logger = LoggerFactory.getLogger(clazz);
        if (logger.isWarnEnabled()) {
            logger.warn(message, args);
        }
    }
    
    // 记录错误日志
    public static void error(Class<?> clazz, String message) {
        Logger logger = LoggerFactory.getLogger(clazz);
        if (logger.isErrorEnabled()) {
            logger.error(message);
        }
    }
    
    // 记录错误日志，带异常
    public static void error(Class<?> clazz, String message, Throwable throwable) {
        Logger logger = LoggerFactory.getLogger(clazz);
        if (logger.isErrorEnabled()) {
            logger.error(message, throwable);
        }
    }
    
    // 记录错误日志，带参数和异常
    public static void error(Class<?> clazz, String message, Throwable throwable, Object... args) {
        Logger logger = LoggerFactory.getLogger(clazz);
        if (logger.isErrorEnabled()) {
            logger.error(message, args, throwable);
        }
    }
    
    // 记录业务操作日志
    public static void businessLog(Class<?> clazz, String userId, String operationType, String businessType, String message) {
        Logger logger = LoggerFactory.getLogger(clazz);
        if (logger.isInfoEnabled()) {
            logger.info("[业务日志] 用户ID: {}, 操作类型: {}, 业务类型: {}, 消息: {}", userId, operationType, businessType, message);
        }
    }
    
    // 简化的业务操作日志
    public static void businessLog(Class<?> clazz, String userId, String operationType, String message) {
        businessLog(clazz, userId, operationType, DEFAULT_BUSINESS_TYPE, message);
    }
    
    // 记录API调用日志
    public static void apiLog(Class<?> clazz, String apiPath, String method, String ip, long responseTime) {
        Logger logger = LoggerFactory.getLogger(clazz);
        if (logger.isInfoEnabled()) {
            logger.info("[API日志] 路径: {}, 方法: {}, IP: {}, 响应时间: {}ms", apiPath, method, ip, responseTime);
        }
    }
    
    // 记录性能日志
    public static void performanceLog(Class<?> clazz, String operation, long executionTime) {
        Logger logger = LoggerFactory.getLogger(clazz);
        if (logger.isInfoEnabled()) {
            logger.info("[性能日志] 操作: {}, 执行时间: {}ms", operation, executionTime);
        }
    }
    
    // 记录参数日志（适用于调试时记录方法参数）
    public static void paramsLog(Class<?> clazz, String methodName, Object... params) {
        Logger logger = LoggerFactory.getLogger(clazz);
        if (logger.isDebugEnabled()) {
            StringBuilder sb = new StringBuilder();
            sb.append("[参数日志] 方法: " + methodName + ", 参数: [");
            for (int i = 0; i < params.length; i++) {
                sb.append(params[i] != null ? params[i].toString() : "null");
                if (i < params.length - 1) {
                    sb.append(", ");
                }
            }
            sb.append("]");
            logger.debug(sb.toString());
        }
    }
    
    // 记录敏感操作日志
    public static void sensitiveLog(Class<?> clazz, String userId, String operation, Map<String, Object> details) {
        Logger logger = LoggerFactory.getLogger(clazz);
        if (logger.isInfoEnabled()) {
            logger.info("[敏感操作] 用户ID: {}, 操作: {}, 详情: {}", userId, operation, details);
        }
    }
}